﻿using System.Threading.Tasks;
using System;
using Microsoft.EntityFrameworkCore;
using Oracle.ManagedDataAccess.Client;
using Simple.Services.System.QHSEViolationCategory.Models;
using System.Xml;

namespace Simple.Services;

public class QHSEOrganizationService
{
    private readonly SimpleDbContext _context;
    private readonly CacheService _cacheService;
    public QHSEOrganizationService(SimpleDbContext context,CacheService cacheService)
    {
        _context = context;
        _cacheService = cacheService;
    }
    public async Task<List<QHSEOrganizationModel>> GetAsync()
    {
        var organizations = await _context.Set<QHSESysOrganization>().ToListAsync();

        return MapperHelper.Map<List<QHSEOrganizationModel>>(organizations);
    }
    public async Task<QHSEOrganizationModel> GetByIdAsync(IdInputModel idInput)
    {
        var violationCategory = await _context.Set<QHSESysOrganization>().FindAsync(idInput.Id);
        if (violationCategory == null)
        {
            return null;
        }
        else
        {
            return MapperHelper.Map<QHSEOrganizationModel>(violationCategory);
        }
    }
    public async Task<List<QHSEOrganizationTreeNodeModel>> GetAsync(QHSEOrganizationInputModel input)
    {
        var query = _context.Set<QHSESysOrganization>().AsQueryable();

        // 根据条件查询
        if (!string.IsNullOrEmpty(input.Name))
        {
            query = query.Where(m => EF.Functions.Like(m.OrganizationName, $"%{input.Name}%"));
        }

        // 排序
        query = query.OrderBy(m => m.DataSort);

        var organizations = await query.ToListAsync();
        var nodes = MapperHelper.Map<List<QHSEOrganizationTreeNodeModel>>(organizations);

        var builder = QHSEOrganizationTreeNodeModel.CreateBuilder(nodes, "0");
        return builder.Build();
    }
    public async Task<int> AddAsync(QHSEOrganizationModel model)
    {
        var organization = MapperHelper.Map<QHSESysOrganization>(model);
        await _context.AddAsync(organization);
        return await _context.SaveChangesAsync();
    }

    public async Task<List<AntTreeNode>> GetTreeAsync()
    {
        var result = await _cacheService.GetOrganizationAsync();
        if (result != null)
        {
            return result;
        }
        var organizations = await _context.Set<QHSESysOrganization>().AsNoTracking().Where(u=>u.RowState == 0).OrderBy(u => u.DataSort).ThenBy(u=>u.OrganizationName).ToListAsync();
        List<TreeNode> nodes = MapperHelper.Map<List<TreeNode>>(organizations);

        var builder = AntTreeNode.CreateBuilder(nodes, "0");
        var data = builder.Build();
        await _cacheService.SetOrganizationAsync(data);
        return data;

    }
    public async Task<List<AntTreeNode>> GetTreeAsync(IdInputModel idInputModel)
    {
        var result = await _cacheService.GetOrganizationAsync(idInputModel);
        if (result != null)
        {
            return result;
        }
        var organizations = await _context.Set<QHSESysOrganization>().AsNoTracking().Where(u => u.RowState == 0).Where(u=>u.NamePath.Contains(idInputModel.Id)).OrderBy(u=> u.DataSort).ThenBy(u => u.OrganizationName).ToListAsync();
        List<TreeNode> nodes = MapperHelper.Map<List<TreeNode>>(organizations);

        var builder = AntTreeNode.CreateBuilder(nodes, "00");
        var data = builder.Build();
        await _cacheService.SetOrganizationAsync(idInputModel,data);
        return data;
    }

    public async Task<int> DeleteAsync(IEnumerable<string> ids)
    {
        var organizations = await _context.Set<QHSESysOrganization>()
            .Where(a => ids.Contains(a.OrganizationId))
            .ToListAsync();

        _context.RemoveRange(organizations);
        int ret = await _context.SaveChangesAsync();
        return ret;
    }

    public async Task<List<QHSESysOrganization>> GetSon(string id)
    {
        var result = await _cacheService.GetQHSEOrganizationSonAsync(id);
        if (result.Count == 0)
        {
            var sons = await _context.Set<QHSESysOrganization>().AsNoTracking().Where(u => u.ParentId == id && u.RowState == 0).ToListAsync();
            await _cacheService.SetQHSEOrganizationSonAsync(sons, id);
            return sons;
        }
        return result;
    }
    public async Task<List<QHSESysOrganization>> GetSon(string id,SimpleDbContext context)
    {
        var result = await _cacheService.GetQHSEOrganizationSonAsync(id);
        if (result.Count == 0)
        {
            var sons = await context.Set<QHSESysOrganization>().AsNoTracking().Where(u => u.ParentId == id && u.RowState == 0).ToListAsync();
            await _cacheService.SetQHSEOrganizationSonAsync(sons, id);
            return sons;
        }
        return result;
    }
}
